<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/12/28 17:36
 * Desc: 景点订单处理
 */
class Service_Ferry_Order
{

    /**
     * 获取订单套餐信息
     * @param $order
     * @return array
     */
    public function get_order_suit($order)
    {
        $has_children = false; // 是否有子订单
        $order_id     = $order['id'];
        $children     = DB::select()
            ->from('member_order')
            ->where('pid', '=', $order_id)
            ->execute()
            ->as_array();
        $data         = array();
        if (!empty($children)) {
            $has_children = true;
            foreach ($children as &$child) {
                $sku = DB::select()->from('member_order_sku')->where('order_id','=',$child['id'])->execute()->get('sku_info');
                if($sku){
                    $sku = json_decode($sku,true);
                    $child['usedate'] = $child['usedate'].' '.$sku['period_time']['start'].'-'.$sku['period_time']['end'];
                }
                $child = array_merge($child, $this->order_data($child));
            }
            $data['children'] = $children;
        } else {
            $sku = DB::select()->from('member_order_sku')->where('order_id','=',$order['id'])->execute()->get('sku_info');
            if($sku){
                $sku = json_decode($sku,true);
                $order['usedate'] = $order['usedate'].' '.$sku['period_time']['start'].'-'.$sku['period_time']['end'];
            }
            $data = $this->order_data($order);
        }

        $data['has_children'] = $has_children;
        return $data;
    }

    /**
     * 订单数据
     * @param $order
     * @return array
     */
    public function order_data($order)
    {
        $suit_id       = $order['suitid'];
        $suit          = $this->suit_info($suit_id);
        $tourers       = Model_Member_Order_Tourer::get_tourer_by_orderid($order['id']);
        $supplier_info = '';
        if ($order['supplierlist']) {
            $supplier_info = $this->supplier_info($order['supplierlist']);
        }
        return compact('suit', 'tourers', 'supplier_info');
    }

    /**
     * 供应商信息
     * @param $supplier_id
     * @return mixed|null
     */
    public function supplier_info($supplier_id)
    {
        return DB::select('suppliername', 'mobile')
            ->from('supplier')
            ->where('id', '=', $supplier_id)
            ->execute()
            ->current();
    }

    /**
     * 套餐信息
     * @param $suit_id
     * @return array|mixed
     */
    public function suit_info($suit_id)
    {
        $suit_info = DB::select()
            ->from('ferry_ticket')
            ->where('id', '=', $suit_id)
            ->execute()
            ->current();
        if (!empty($suit_info)) {
            $suit_info['ticket_type_name'] = Model_Ferry_Ticket_Type::get_type_name($suit_info['tickettypeid']);
        }
        return $suit_info;
    }
}